﻿namespace Blaze.FX
{
    using UnityEngine;

    /// <summary>
    /// 贴图模糊静态效果。
    /// </summary>
    public class TextureBlurStaticEffect : MonoBehaviour
    {
        [Range(0.1f, 1)]
        public float SampleLevel = 0.125f;

        [Range(0, 20)]
        public int Strengh = 5;

        private void Start()
        {
            var meshRenderer = GetComponent<MeshRenderer>();
            var texture = (Texture2D) meshRenderer.sharedMaterial.mainTexture;
            var bluredTexture = Instantiate(texture);
            var width = (int) (texture.width * SampleLevel);
            var height = (int) (texture.height * SampleLevel);
            TextureScale.Point(bluredTexture, width, height);
            var pixels = bluredTexture.GetPixels32();
            var stackBlur = new StackBlur(bluredTexture.width, bluredTexture.height, Strengh);
            stackBlur.Apply(pixels, Strengh);
            bluredTexture.SetPixels32(pixels);
            bluredTexture.Apply(false, true);

            meshRenderer.material = new Material(meshRenderer.sharedMaterial)
            {
                mainTexture = bluredTexture
            };
        }
    }
}